package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.VipModel;
import com.jp.knowledge.util.ImageLoadUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/6.
 */

public class UserVipAdapter extends RecycleVAdapter<VipModel> {

    public UserVipAdapter(Context context, @NonNull List<VipModel> list) {
        super(context, list);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_user_vip;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        VipModel data = getData(position);
        List<VipModel.ListBean> listBean = data.getList();
        if(listBean == null)
            listBean = new ArrayList<>();
        for(int i = listBean.size(); i < 2; i++)
            listBean.add(new VipModel.ListBean());

        TextView serviceTv = holder.getTextView(R.id.service_tv);
        ImageView serviceIv = holder.getImageView(R.id.service_iv);
        TextView normalTv = holder.getTextView(R.id.normal_tv);
        ImageView normalIv = holder.getImageView(R.id.normal_iv);
        TextView vipTv = holder.getTextView(R.id.vip_tv);
        ImageView vipIv = holder.getImageView(R.id.vip_rv);

        setItemView(serviceTv, data.getTitle(), data.getColor(), serviceIv, data.getIcon());
        VipModel.ListBean normalBean = listBean.get(0);
        setItemView(normalTv, normalBean.getInfo(), normalBean.getColor(), normalIv, normalBean.getIcon());
        VipModel.ListBean vipBean = listBean.get(1);
        setItemView(vipTv, vipBean.getInfo(), vipBean.getColor(), vipIv, vipBean.getIcon());
    }

    protected void setItemView(TextView textView, String tvString, String tvColor, ImageView imageView, String ivUrl){
        if(isStringEmpty(tvString)){
            textView.setVisibility(View.GONE);
        } else{
            textView.setVisibility(View.VISIBLE);
            textView.setText(tvString);
            setTextColor(textView, tvColor);
        }
        if(isStringEmpty(ivUrl)){
            imageView.setVisibility(View.GONE);
        } else{
            imageView.setVisibility(View.VISIBLE);
            ImageLoadUtil.load(mContext, ivUrl, imageView);
        }
    }

    protected boolean isStringEmpty(String str){
        if(str == null || str.trim().length() == 0)
            return true;
        return false;
    }

    protected void setTextColor(TextView textView, String color){
        try{
            textView.setTextColor(Color.parseColor(color));
        } catch (Exception e){
            textView.setTextColor(mContext.getResources().getColor(R.color.font_gray));
        }
    }
}
